package jsu.xxy.coffee3.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jsu.xxy.coffee3.domain.*;
import jsu.xxy.coffee3.mapper.MaterialMapper;
import jsu.xxy.coffee3.mapper.ProductMaterialMapper;
import jsu.xxy.coffee3.service.ProductService;
import jsu.xxy.coffee3.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @author 张从轩
* @description 针对表【product(商品信息表)】的数据库操作Service实现
* @createDate 2023-06-03 06:28:03
*/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
implements ProductService{
    @Autowired
    MaterialMapper materialMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductMaterialMapper productMaterialMapper;

    @Override
    public List<Product> getAll() {
        List<Product> l =  productMapper.selectAll();
        for(Product p : l){
            List<Double> list = productMapper.getCount(p.getProductid());
            if (list.size() == 0) continue;
            double min = list.get(0);
            for(double m : list){
                if(m > min) m = min;
            }
            p.setCount((int) min);
        }
        return l;
    }

    @Override
    public Page<ProductMaterial> getAllProduct(Integer pageNo, Integer pageSize) {

        List<ProductMaterial> list = productMapper.getAllOnlyProduct((pageNo-1)*pageSize,pageSize);
        for(ProductMaterial l : list){
            l.setMaterial(materialMapper.selectByproductId(l.getProductid()));
        }
        Page<ProductMaterial> page = new Page<>();
        page.setTotal(productMapper.getAllProductcount());
        page.setRecords(list);
        return page;
    }
    @Override
    public Page<ProductMaterial> getProductByname(String name, Integer pageNo, Integer pageSize) {
        List<ProductMaterial> list = productMapper.getAllOnlyProductByName(name,(pageNo-1)*pageSize,pageSize);
        for(ProductMaterial l : list){
            l.setMaterial(materialMapper.selectByproductId(l.getProductid()));
        }
        Page<ProductMaterial> page = new Page<>();
        page.setTotal(productMapper.getCountByName(name));
        page.setRecords(list);
        return page;
    }
    @Override
    public Page<ProductMaterial> getProductById(int id, Integer pageNo, Integer pageSize) {
        List<ProductMaterial> list = productMapper.getAllOnlyProductById(id,(pageNo-1)*pageSize,pageSize);
        for(ProductMaterial l : list){
            l.setMaterial(materialMapper.selectByproductId(l.getProductid()));
        }
        Page<ProductMaterial> page = new Page<>();
        page.setTotal(productMapper.getCountById(id));
        page.setRecords(list);
        return page;
    }
    @Override
    public int addProduct(ProductMaterial productMaterials) {
        int productid = 0;
        int sum =0;
//        没有商品
        if(productMapper.getCountByName(productMaterials.getName()) == 0) {
            productMapper.addProduct(productMaterials);
            productid = productMapper.getByName(productMaterials.getName());
            sum ++;
        }
//
        for (MaterialNoId l:productMaterials.getMaterial()){
            if(materialMapper.selectByName(l.getMname())==null){
                materialMapper.insertMaterial(l);
                sum ++;
            }
            int mid = materialMapper.selectByName(l.getMname()).getMid();
            if(productMaterialMapper.getByMidAndProductId(mid , productid) == 0){
                productMaterialMapper.add(mid , productid , l.getComnum());
                sum ++;
            }

        }

        return sum;

    }
    @Override
    public int changeProduct(ProductMaterial productMaterials) {
        System.out.println(productMaterials);
        int productid = productMaterials.getProductid();
        int sum = 0;
        productMapper.setAll(productMaterials.getProductid(),productMaterials.getName(),productMaterials.getPrice(),productMaterials.getDescription(),productMaterials.getImage(),productMaterials.getCategoryid());
        sum ++;

        productMaterialMapper.deleteByMidAndProductid(productid);
        for (MaterialNoId l:productMaterials.getMaterial()){
            System.out.println(l);
            if(materialMapper.selectByName(l.getMname())==null){
                materialMapper.insertMaterial(l);
                sum ++;
            }
            int mid = materialMapper.selectByName(l.getMname()).getMid();
            if(productMaterialMapper.getByMidAndProductId(mid , productid) == 0){
//                System.out.println(l.getMna);
                productMaterialMapper.add(mid , productid , l.getComnum());
                sum ++;
            }
        }
        return sum;
    }
    @Override
    public Page<ProductMaterial> getProductByCategory(int category, Integer pageNo, Integer pageSize) {
        List<ProductMaterial> list = productMapper.getAllOnlyProductByCategory(category,(pageNo-1)*pageSize,pageSize);
        for(ProductMaterial l : list){
            l.setMaterial(materialMapper.selectByproductId(l.getProductid()));
        }
        Page<ProductMaterial> page = new Page<>();
        page.setTotal(productMapper.getCountByCategory(category));
        page.setRecords(list);
        return page;
    }

    @Override
    public boolean delete(Integer id) {
        return  productMapper.deleteById(id) > 0 &&productMaterialMapper.deleteById(id) > 0;

    }

    @Override
    public int deletes(List<Integer> ids) {
        int ans =0;
        for(int l : ids){
            ans += productMaterialMapper.deleteById(l) ;
            ans +=productMapper.deleteById(l);
        }
        return ans;
    }

    @Override
    public List<ProductMaterial> getAllProductNoPage() {
        List<ProductMaterial> list = productMapper.getAllOnlyProductNoPage();
        for(ProductMaterial l : list){
            l.setMaterial(materialMapper.selectByproductId(l.getProductid()));
        }
        return list;

    }

}
